package slidingwindow;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by fengliejv on 2017/10/10.
 */
public class LongestSubstringWithoutRepeatingCharacters3 {
    public int lengthOfLongestSubstring(String s) {
        Set<Character> set = new HashSet<>();
        int quick = 0;
        int slow = 0;
        int result = 0;
        while (quick < s.length()) {
            while (set.contains(s.charAt(quick))) {
                set.remove(s.charAt(slow));
                slow++;
            }
            set.add(s.charAt(quick));
            quick++;
            result = Math.max(result, set.size());
        }
        return result;
    }


//    public int lengthOfLongestSubstring(String s) {
//        char[] inputs = s.toCharArray();
//        HashMap<Character , Character> map = new HashMap<Character, Character>();
//        int max=0;
//        int count=0;
//        for(int i=0;i<inputs.length;i++){
//            if(map.containsKey(inputs[i])){
//                max=(count>max)?count:max;
//                i=i-count+1;
//                map.clear();
//                count=0;
//
//            }
//            map.put(inputs[i], inputs[i]);
//            count++;
//        }
//        max=(count>max)?count:max;
//        return max;
//    }
}
